package cn.jtfadmin.base.shiro.filters;


import cn.jtfadmin.base.shiro.config.ShiroCustomerFilter;
import cn.jtfadmin.base.shiro.config.ShiroGlobalFilterConfig;
import cn.jtfadmin.base.shiro.config.ShiroWebFilterConfig;
import org.apache.shiro.web.servlet.OncePerRequestFilter;
import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/**
 * redirectUrl encode 中增加jession问题
 *
 * @author jtf
 * @see ShiroCustomerFilter
 * @see ShiroGlobalFilterConfig
 * @see ShiroWebFilterConfig
 * @since 0.0.1
 */
@Component
public class ShiroResponseEncodeUrlDisableFilter implements ShiroCustomerFilter, ShiroGlobalFilterConfig {


    public static final String NAME = "responseEncodeUrlDisable";

    @Override
    public String getName() {
        return NAME;
    }

    @Override
    public Filter getFilter() {
        return new OncePerRequestFilter() {
            @Override
            protected void doFilterInternal(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws ServletException, IOException {
                if (servletRequest instanceof HttpServletRequest) {
                   servletRequest.setAttribute(ShiroHttpServletRequest.SESSION_ID_URL_REWRITING_ENABLED, Boolean.FALSE);
                }
                filterChain.doFilter(servletRequest, servletResponse);
            }
        };
    }

    @Override
    public Set<String> getGlobalFilterNames() {
        Set<String> set = new HashSet<>();
        set.add(getName());
        return set;
    }


}

